package com.qire.manhua.model.viewModel.impl;

import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;

import com.qire.common.collect.ListUtil;
import com.qire.common.support.base.BaseViewModel;
import com.qire.common.support.base.IViewModel;
import com.qire.common.utils.EquipmentInfoUtil;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.common.widget.adapter.auto.SummerBindAdapter;
import com.qire.common.widget.adapter.auto.annotation.AdapterEntityBind;
import com.qire.manhua.R;
import com.qire.manhua.activity.ComicRankListOfVipActivity;
import com.qire.manhua.controller.ComicRankListOfVipController;
import com.qire.manhua.dal.service.ComicRankListOfVipService;
import com.qire.manhua.databinding.ActivityComicRankListOfVipBinding;
import com.qire.manhua.databinding.LayoutComicRankListOfVipItemBinding;
import com.qire.manhua.view.adapter.pager.ComicRankListPagerAdapter;
import com.qire.manhua.view.attributeBinder.attribute.ImageAttribute;

import androidx.databinding.BaseObservable;
import androidx.databinding.library.baseAdapters.BR;

public class ComicRankListOfVipViewModel extends BaseViewModel implements IViewModel {

    private final ComicRankListOfVipService rankService = new ComicRankListOfVipService(this);

    private final CharSequence[] tabTitleArray = {"周榜", "月榜", "总榜"};
    private final SummerBindAdapter<ComicRankListOfVipItemViewModel>[] rankVipAdapterArray = new SummerBindAdapter[] {
            new SummerBindAdapter(ListUtil.newArrayList()),
            new SummerBindAdapter(ListUtil.newArrayList()),
            new SummerBindAdapter(ListUtil.newArrayList())
    };
    private final ComicRankListPagerAdapter pagerAdapter = new ComicRankListPagerAdapter(tabTitleArray);

    private int currentTabIndex = 0;

    @Override
    public int getVariableId() {
        return BR.comicRankListOfVipViewModel;
    }

    //region    #附加管理 begin
    /**
     * 附加属性绑定
     * @param activity 归属页面
     * @param controller 事件控制器
     */
    public void onAddition(ComicRankListOfVipActivity activity, ComicRankListOfVipController controller) {
        for(SummerBindAdapter adapter : rankVipAdapterArray) {
            ListView listView = new ListView(activity);
            listView.setLayoutParams(new ViewGroup.MarginLayoutParams(-1, -2));
            // setDivider 会重置 setDividerHeight 所以 setDividerHeight必须先调用
            listView.setDivider(null);
            listView.setDividerHeight(EquipmentInfoUtil.dip2px(15));
            listView.setAdapter(adapter);
            // 首尾留白
            View footerView = new View(activity);
            footerView.setLayoutParams(new AbsListView.LayoutParams(-1, EquipmentInfoUtil.dip2px(28)));
            listView.addFooterView(footerView);
            View headerView = new View(activity);
            headerView.setLayoutParams(new AbsListView.LayoutParams(-1, EquipmentInfoUtil.dip2px(15)));
            listView.addHeaderView(headerView);
            // 添加 pager子View
            pagerAdapter.addPagerView(listView);
            adapter.setEventProcess(controller);
        }

        ActivityComicRankListOfVipBinding viewDataBinder = activity.viewDataBinder();
        viewDataBinder.comicRankVipViewPager.setAdapter(pagerAdapter);
        viewDataBinder.comicRankVipTabLayout.setViewPager(viewDataBinder.comicRankVipViewPager);

    }

    /**
     * 清理函数，在Activity退出时，ViewModel生命周期到期会被回收，调用该事件，
     * 页面由于切换、横竖屏转换、内存回收等情况并不会影响到ViewModel的生命周期。
     */
    @Override
    protected void onCleared() {
        super.onCleared();
    }
    //endregion #附加管理 end

    //region    #业务数据 begin
    public void loadAllTabData() {
        rankService.loadNextTabData();
    }

    public boolean setCurrentTabIndex(int tabIndex) {
        if(tabIndex < 0 || tabIndex > (tabTitleArray.length - 1)) {
            return false;
        }
        this.currentTabIndex = tabIndex;
        return true;
    }

    public void addRankItemViewModel(int comicId, ImageAttribute comicCover, CharSequence floatLayerText, CharSequence comicName, CharSequence comicAuthor,
                                     CharSequence comicBrief, CharSequence comicLabel, CharSequence comicPopularity, int comicSerialNumber) {
        rankVipAdapterArray[currentTabIndex].getDataList().add(new ComicRankListOfVipItemViewModel(comicId, comicCover,
                floatLayerText, comicName, comicAuthor, comicBrief, comicLabel, comicPopularity, comicSerialNumber));
    }

    public void refreshRankUI() {
        rankVipAdapterArray[currentTabIndex].notifyDataSetChanged();
    }
    //endregion #业务数据 end

    private static final int[] rankingIcons = {R.drawable.app_comic_rank_list_item_no1, R.drawable.app_comic_rank_list_item_no2, R.drawable.app_comic_rank_list_item_no3};

    @AdapterEntityBind(viewRid = R.layout.layout_comic_rank_list_of_vip_item)
    public class ComicRankListOfVipItemViewModel extends BaseObservable implements AutoVariable<LayoutComicRankListOfVipItemBinding> {

        public final int comicId;
        public final ImageAttribute comicCover;
        public final CharSequence floatLayerText;
        public final CharSequence comicName;
        public final CharSequence comicAuthor;
        public final CharSequence comicBrief;
        public final CharSequence comicLabel;
        public final CharSequence comicPopularity;
        public final int comicSerialNumber;

        public ComicRankListOfVipItemViewModel(int comicId, ImageAttribute comicCover, CharSequence floatLayerText, CharSequence comicName, CharSequence comicAuthor,
                                               CharSequence comicBrief, CharSequence comicLabel, CharSequence comicPopularity, int comicSerialNumber) {
            this.comicId            = comicId;
            this.comicCover         = comicCover;
            this.floatLayerText     = floatLayerText;
            this.comicName          = comicName;
            this.comicAuthor        = comicAuthor;
            this.comicBrief         = comicBrief;
            this.comicLabel         = comicLabel;
            this.comicPopularity    = comicPopularity;
            this.comicSerialNumber  = comicSerialNumber;
        }

        @Override
        public void bindVariable(LayoutComicRankListOfVipItemBinding viewBind) {
            viewBind.setComicRankListOfVipItemViewModel(this);
        }

        public CharSequence getComicSerialNumber() {
            if(comicSerialNumber <= 3) {
                return "";
            }
            return String.valueOf(comicSerialNumber);
        }

        public int getComicSerialNumberBgRid() {
            if(comicSerialNumber <= 3) {
                return rankingIcons[comicSerialNumber - 1];
            }
            return R.drawable.app_comic_rank_list_item_no;
        }

    }

}
